package com.xdtech.forum.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.PageRequest;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.forum.dao.CommentInfoDao;
import com.xdtech.forum.model.Article;
import com.xdtech.forum.model.CommentInfo;
import com.xdtech.forum.model.Member;
import com.xdtech.forum.service.CommentInfoService;
import com.xdtech.forum.vo.CommentInfoItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-06 16:52:46
 * @since 1.0
 * @see
 */
@Service
public class CommentInfoServiceImpl implements CommentInfoService {
	@Autowired
	private CommentInfoDao commentInfoDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param entity
	 */
	public void save(CommentInfo entity) {
		commentInfoDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<CommentInfo> entities) {
		commentInfoDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param entity
	 */
	public void delete(CommentInfo entity) {
		commentInfoDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		commentInfoDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param id
	 * @return
	 */
	public CommentInfo get(Long id) {
		return commentInfoDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @return
	 */
	public List<CommentInfo> getAll() {
		return commentInfoDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<CommentInfoItem> commentInfos = null;
		long rows = 0;
		if (pg!=null) {
			Page<CommentInfo> page = commentInfoDao.findPage(pg,"from CommentInfo order by createTime desc", values);
			commentInfos = CommentInfoItem.createItems(page.getResult());
			rows = page.getTotalItems();
		}else {
			List<CommentInfo> commentInfoList = commentInfoDao.find("from CommentInfo order by id desc", values);
			commentInfos = CommentInfoItem.createItems(commentInfoList);
			rows = commentInfos.size();
		}
		results.put("total", rows);
		results.put("rows", commentInfos);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateCommentInfo(CommentInfoItem item) {
		CommentInfo commentInfo = null;
		if (item.getId()==null) {
			commentInfo = new CommentInfo();
		}else {
			commentInfo = commentInfoDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(commentInfo, item);
		commentInfoDao.save(commentInfo);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param commentInfoId
	 * @return
	 */
	public CommentInfoItem loadCommentInfoItem(Long commentInfoId) {
		CommentInfo commentInfo = commentInfoDao.get(commentInfoId);
		CommentInfoItem commentInfoItem = new CommentInfoItem();
		BeanUtils.copyProperties(commentInfoItem, commentInfo);
		return commentInfoItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteCommentInfoInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-06 16:52:46
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteCommentInfoInfo(List<Long> commentInfoIds) {
		for (Long id : commentInfoIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-6下午10:21:57
	 * @modified by
	 * @return
	 */
	@Override
	public List<CommentInfoItem> loadTop5Items() {
		Page<CommentInfo> commentPage = commentInfoDao.findPage(new PageRequest(1, 5), "from CommentInfo order by createTime desc");
		return CommentInfoItem.createItems(commentPage.getResult());
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-7下午4:23:00
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public List<CommentInfoItem> loadCommentInfosByMemberId(Long id) {
		List<CommentInfo> commentInfos = commentInfoDao.findByHql("from CommentInfo ci where ci.member.id=?",id);
		return CommentInfoItem.createItems(commentInfos);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-8下午8:30:40
	 * @modified by
	 * @param articleId
	 * @param content
	 * @param memberId
	 */
	@Override
	public void saveMemberPostComment(Long articleId, String content, Long memberId) {
		CommentInfo commentInfo = new CommentInfo();
		commentInfo.setContent(content);
		commentInfo.setMember(new Member(memberId));
		commentInfo.setArticle(new Article(articleId));
		save(commentInfo);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-8下午9:05:43
	 * @modified by
	 * @param articleId
	 * @return
	 */
	@Override
	public Map<String, Object> loadArticleComments(Long articleId,Pagination pg) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<CommentInfoItem> commentInfos = null;
		long rows = 0;
		if (pg!=null) {
			Page<CommentInfo> page = commentInfoDao.findPage(pg,"from CommentInfo where article.id=? order by createTime desc", articleId);
			commentInfos = CommentInfoItem.createItems(page.getResult());
			rows = page.getTotalItems();
		}else {
			List<CommentInfo> commentInfoList = commentInfoDao.findByHql("from CommentInfo where article.id=? order by id desc", articleId);
			commentInfos = CommentInfoItem.createItems(commentInfoList);
			rows = commentInfos.size();
		}
		results.put("total", rows);
		results.put("rows", commentInfos);
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:06
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
